import '../../public.dart';

class MinePage extends StatefulWidget {
  const MinePage({super.key});

  @override
  State<MinePage> createState() => _MinePageState();
}

class _MinePageState extends State<MinePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: SafeArea(
        child: ProviderWidget<UserModel>(
          builder: (context, model, child) {
            return SingleChildScrollView(
              child: Column(
                children: [
                  // 顶部用户信息区域
                  _buildUserInfoSection(model),

                  // 等级选择器
                  _buildLevelSelector(model.userInfo?.levelName ?? 'Bronze'),

                  // 功能列表
                  _buildFeatureList(),

                  // 退出登录和删除账户
                  _buildLogoutSection(),
                ],
              ),
            );
          },
          model: Provider.of<UserModel>(context),
          onModelReady: (model) async {
            await model.getUserInfo();
          },
        ),
      ),
    );
  }

  // 用户信息区域
  Widget _buildUserInfoSection(UserModel model) {
    return Column(
      children: [
        const SizedBox(height: 20),
        // 头像
        Center(
          child: Container(
            width: 80,
            height: 80,
            decoration: const BoxDecoration(
              color: Colors.grey,
              shape: BoxShape.circle,
            ),
            child: const Icon(Icons.person, size: 40, color: Colors.white),
          ),
        ),
        const SizedBox(height: 12),
        // 用户名
        Text(
          model.userInfo?.userName ?? 'flutter',
          style: const TextStyle(
            fontSize: 18,
            fontWeight: FontWeight.w600,
            color: Color(0xFF323232),
          ),
        ),
        const SizedBox(height: 24),
      ],
    );
  }

  // 等级选择器
  Widget _buildLevelSelector(String currentLevel) {
    List<String> levels = ['Bronze', 'Silver', 'Gold', 'Platinum', 'Diamond'];
    List<IconData> levelIcons = [
      Icons.ac_unit_outlined,
      Icons.star_outline,
      Icons.workspace_premium_outlined,
      Icons.workspace_premium,
      Icons.diamond,
    ];

    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 31),
      margin: const EdgeInsets.only(bottom: 24),
      child: SingleChildScrollView(
        scrollDirection: Axis.horizontal,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: List.generate(levels.length, (index) {
            bool isActive = levels[index] == currentLevel;
            return Container(
              margin: const EdgeInsets.symmetric(horizontal: 8),
              padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
              decoration: BoxDecoration(
                color: isActive ? const Color(0xFF1DE9B6) : Colors.transparent,
                borderRadius: BorderRadius.circular(20),
                border: Border.all(
                  color: isActive ? const Color(0xFF1DE9B6) : Colors.grey,
                  width: 1,
                ),
              ),
              child: Row(
                children: [
                  Icon(
                    levelIcons[index],
                    size: 16,
                    color: isActive ? Colors.white : Colors.grey,
                  ),
                  const SizedBox(width: 6),
                  Text(
                    levels[index],
                    style: TextStyle(
                      fontSize: 14,
                      fontWeight: FontWeight.w500,
                      color: isActive ? Colors.white : Colors.grey,
                    ),
                  ),
                ],
              ),
            );
          }),
        ),
      ),
    );
  }

  // 功能列表
  Widget _buildFeatureList() {
    List<Map<String, dynamic>> features = [
      {
        'icon': Icons.person_outline,
        'title': 'Profile details',
        'color': const Color(0xFF257CEE),
      },
      {
        'icon': Icons.lock_outline,
        'title': 'Security',
        'color': const Color(0xFF257CEE),
      },
      {
        'icon': Icons.help_outline,
        'title': 'Support',
        'color': const Color(0xFF257CEE),
      },
      {
        'icon': Icons.verified_user_outlined,
        'title': 'KYC verification',
        'color': const Color(0xFF257CEE),
      },
      {
        'icon': Icons.people_outline,
        'title': 'Referral',
        'color': const Color(0xFF257CEE),
      },
    ];

    return Column(
      children: List.generate(features.length, (index) {
        return _buildFeatureItem(
          features[index]['icon'],
          features[index]['title'],
          features[index]['color'],
        );
      }),
    );
  }

  // 功能项
  Widget _buildFeatureItem(IconData icon, String title, Color color) {
    return GestureDetector(
      onTap: () {
        // 处理点击事件
        print('点击了 $title');
        if (title == 'Profile details') {
          // 跳转到用户信息页面
        } else if (title == 'Security') {
          // 跳转到安全设置页面
          RouterHelper.navigator.pushNamed(Routers.security);
        }
      },
      child: Container(
        padding: const EdgeInsets.symmetric(horizontal: 31, vertical: 16),
        decoration: const BoxDecoration(
          border: Border(
            bottom: BorderSide(color: Color(0xFFF0F0F0), width: 1),
          ),
        ),
        child: Row(
          children: [
            Icon(icon, size: 20, color: color),
            const SizedBox(width: 12),
            Text(
              title,
              style: const TextStyle(fontSize: 16, color: Color(0xFF323232)),
            ),
            const Spacer(),
            Icon(
              Icons.chevron_right,
              size: 20,
              color: Colors.grey.withOpacity(0.5),
            ),
          ],
        ),
      ),
    );
  }

  // 退出登录和删除账户区域
  Widget _buildLogoutSection() {
    return Column(
      children: [
        // 退出登录
        GestureDetector(
          onTap: () {
            // 退出登录逻辑
            _handleLogout();
          },
          child: Container(
            padding: const EdgeInsets.symmetric(horizontal: 31, vertical: 16),
            decoration: const BoxDecoration(
              border: Border(
                bottom: BorderSide(color: Color(0xFFF0F0F0), width: 1),
              ),
            ),
            child: Row(
              children: [
                Icon(Icons.logout, size: 20, color: Colors.red),
                const SizedBox(width: 12),
                Text(
                  'Sign Out',
                  style: const TextStyle(fontSize: 16, color: Colors.red),
                ),
                const Spacer(),
                Icon(
                  Icons.chevron_right,
                  size: 20,
                  color: Colors.grey.withOpacity(0.5),
                ),
              ],
            ),
          ),
        ),

        // 删除账户
        GestureDetector(
          onTap: () {
            // 删除账户逻辑
            _handleDeleteAccount();
          },
          child: Container(
            padding: const EdgeInsets.symmetric(horizontal: 31, vertical: 16),
            child: Row(
              children: [
                Icon(Icons.delete_outline, size: 20, color: Colors.red),
                const SizedBox(width: 12),
                Text(
                  'Delete Account',
                  style: const TextStyle(fontSize: 16, color: Colors.red),
                ),
                const Spacer(),
                Icon(
                  Icons.chevron_right,
                  size: 20,
                  color: Colors.grey.withOpacity(0.5),
                ),
              ],
            ),
          ),
        ),
      ],
    );
  }

  // 退出登录处理
  void _handleLogout() {
    // Toast.dialog(
    //   title: 'Sign Out',
    //   content: 'Are you sure you want to sign out?',
    //   confirmText: 'Sign Out',
    //   showCancel: true,
    //   confirm: () {
    //     // 执行退出登录
    //     StorageManager.clear();
    //     Navigator.pop(context);
    //     // 跳转到登录页面
    //     RouterHelper.navigator.pushNamedAndRemoveUntil(
    //       Routers.login,
    //       (route) => false,
    //     );
    //   },
    // );
    BrnDialogManager.showConfirmDialog(
      context,
      title: 'Sign Out',
      message: 'Are you sure you want to sign out?',
      cancel: 'Sign Out',
      confirm: 'Confirm',
      onCancel: () {
        Navigator.of(context).pop();
      },
      onConfirm: () {
        // 执行退出登录
        StorageManager.clear();
        Navigator.pop(context);
        // 跳转到登录页面
        RouterHelper.navigator.pushNamedAndRemoveUntil(
          Routers.login,
          (route) => false,
        );
      },
    );
  }

  // 删除账户处理
  void _handleDeleteAccount() {
    showDialog(
      context: context,
      builder: (context) => AlertDialog(
        title: const Text('Delete Account'),
        content: const Text(
          'Are you sure you want to delete your account? This action cannot be undone.',
        ),
        actions: [
          TextButton(
            onPressed: () => Navigator.pop(context),
            child: const Text('Cancel'),
          ),
          TextButton(
            onPressed: () {
              // 执行删除账户逻辑
              Navigator.pop(context);
              // 这里可以添加删除账户的API调用
            },
            child: const Text('Delete'),
          ),
        ],
      ),
    );
  }
}
